
<template>
  <div>
    <!-- 表格 -->
    <el-table
      :tree-props="treeProps"
      :row-key="rowKey"
      :data="data"
      border
      stripe
      style="width: 100%"
    >
      <el-table-column
        v-for="(item, index) in cols"
        :key="index"
        :type="item.type"
        :label="item.title"
        :prop="item.name"
        :width="item.width"
        :align="item.align"
      >
        <template v-if="item.slot" v-slot="{ row, $index, column }">
          <slot
            :name="item.slot"
            :row="row"
            :index="$index"
            :column="column"
          ></slot> </template
      ></el-table-column>
    </el-table>
    <!-- 分页  -->
    <el-pagination
      class="pagination"
      v-if="page.pagesize"
      @size-change="(size) => change('pagesize', size)"
      @current-change="(num) => change('pagenum', num)"
      :current-page="page.pagenum"
      :page-size="page.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
export default {
  name: "Table",
  methods: {
    /**
     * 分页或者每页显示条数变更事件
     */
    //  type = pagenum
    //  type = pagesize
    change(type, value) {
      // this.$emit("input", { ...this.value, [type]: value });
      const obj = { ...this.value, [type]: value };
      this.$emit("change", obj);
      this.$emit("input", obj);
    },
  },
  computed: {
    /**
     * 分页
     */
    page() {
      return this.value || {};
    },
  },
  props: {
    /**
     * 列
     */
    cols: {
      required: true,
      type: Array,
      default: () => {},
    },
    /**
     * 树形数据结构
     */
    treeProps: {
      type: Object,
      default: () => ({ children: "children", hasChildren: "hasChildren" }),
    },
    /**
     * 数据
     */
    data: {
      required: true,
      type: Array,
      default: () => {},
    },
    /**
     * 对应table的row-key
     */
    rowKey: {
      type: String,
      default: "id",
    },
    /**
     * 分页  如果需要支持v-model，那么接收的值必须是value
     */
    value: {
      type: Object,
      default: () => {},
    },
    /**
     * 总条数
     */
    total: Number,
  },
  data() {
    return {};
  }
};
</script>

<style lang='less' scoped>
.pagination {
  margin-top: 15px;
}
</style>